<?php 
class companiesAction extends AdminMaster{
	public function preExecute(){
		$this->formFilter['filter_staff_number_id'] = '-1';
		$this->formFilter['filter_location_city'] = '-1';
		$this->data['filter_form_staff_number']		= MasterCompanyStaffNumbersTable::getDataList(array('filter_enabled'=>'1'));
		$this->data['filter_location_city']		= LocationCitiesTable::getDataList(array('filter_enabled'=>'1'));
		parent::preExecute();
	}
	public function execute($request){
		$this->data['navigation']	= __('module_companies',null,'menu').' › '. __('module_companies_action_companies',null,'menu');
		$this->formData				= $request->getParameter('Companies');	//get form data from add new || edit page
		$this->data['child']['position']	= CompanyPersonContactPositionsTable::getDataList(array('filter_enabled'=> '1'));
		$this->data['child']['personContacts']	= $request->getParameter('personContacts');
		
		switch ($this->data['action']){
			case ADMIN_DEFINED::DELETE_RECORD:
				$this->delRecord();
				break;
			case ADMIN_DEFINED::DISABLE:
				$this->disableRecord();
				break;
			case ADMIN_DEFINED::ENABLE:
				$this->enableRecord();
				break;
			case ADMIN_DEFINED::ADD_RECORD:
				$this->addRecord();
				break;
			case ADMIN_DEFINED::EDIT_RECORD:
				$this->editRecord();
				break;
			case ADMIN_DEFINED::SAVE_ORDER:
				$this->saveOrder();
				break;
			#MORE ACTION HERE
			default:
				$this->filterList();
				break;
		}

		$this->setVar('data', $this->data);
	}
	private function filterList(){
		$this->setAction();
		$this->data['navigation'].=' › '. __('nav_list',null,'table_list');
		$this->data['list']			 		= CompaniesTable::getDataList($this->data['filter'], $this->data['pagination']);
		
		$this->data['pagination']['total']		= CompaniesTable::getDataListCount($this->data['filter']);
		$this->data['pagination']['router']		= 'major_list';
		$this->data['pagination']['params']		= $this->data['filter'];
		$pag = new PaginationHelper2($this->data['pagination']);
		$this->data['pagination_pager']	= $pag->getPaginations();
		if($this->data['filter']['filter_key'] == '*'){
			$this->data['filter']['filter_key'] = '';
		}
		
	}
	private function delRecord(){
		//checking user
		CompaniesTable::delete($this->data['select_list']);
		$this->filterList();
	}
	private function disableRecord(){
		//checking user
		CompaniesTable::disable($this->data['select_list']);
		$this->filterList();
	}
	private function enableRecord(){
		//checking user
		CompaniesTable::enable($this->data['select_list']);
		$this->filterList();
	}
	private function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		Companies::saveSortOrder($sortData);
		$this->filterList();
	}
	/*********************************************************************************
	*
	* ADD NEW
	* 
	**********************************************************************************/
	private function addRecord(){
		$this->data['navigation'].=' › '. __('nav_add_new',null,'table_list');
		$this->data['button_action'] = __('button_add',null,'table_list');
		$this->setTemplate('companiesForm');

		$form = new CompaniesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$product_id = CompaniesTable::insert($this->formData);
				if($product_id > 0){
					$this->addPersonContact($product_id);
					$this->data['result']	= __('add_new_success', null, 'company_companies');
					$form = new CompaniesFForm();
				}else{
					$this->data['result']	= sprintf(__('add_new_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}
		}
		$this->setVar('form', $form);
	}
	private function addPersonContact($company_id = 0){
		$company_id = intval($company_id);
		if($company_id <=0){
			return;
		}
		CompanyPersonContactsTable::deleteByCompanyId($company_id);
		foreach($this->data['child']['personContacts'] as $val){
			$arr = explode(';', $val);
			if(count($arr) >=2){
				CompanyPersonContactsTable::insert(array(
					'company_id'		=> $company_id,
					'position_id'		=> $arr[1],
					'name'				=> $arr[0],
					'email'				=> $arr[2],
					'phone_number'		=> $arr[3]
				));
			}
		}
	}
	/*
	 * EDIT
	*/
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit',null,'table_list');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('button_edit',null,'table_list');
		$this->setTemplate('companiesForm');

		$form = new CompaniesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(CompaniesTable::update($this->formData)){
					$this->addPersonContact($this->formData['id']);
					$this->data['result']	= __('edit_success', null, 'company_companies');
					$this->data['child']['person_contact'] = CompanyPersonContactsTable::getDataList(array('filter_enabled' => '1', 'filter_company_id'=>$this->data['id']));
					
				}else{
					$this->data['result']	= sprintf(__('edit_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}				
		}else{
			$data = CompaniesTable::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				
				$this->formData = array(
						'staff_number_id'			=> $data['staff_number_id'],
						'company_name'				=> $data['company_name'],
						'company_description'		=> $data['company_description'],
						'address'					=> $data['address'],
						'website'					=> $data['website'],
						'email'						=> $data['email'],
						'phone_number'				=> $data['phone_number'],
						'fax_number'				=> $data['fax_number'],
						'is_in_longduc'				=> $data['is_in_longduc'],
						'location_city_id'			=> $data['location_city_id'],
						'location_district_id'		=> $data['location_district_id'],
						'is_enabled'				=> $data['is_enabled'],
						'_csrf_token'				=> $form->getCSRFToken()
				);
				$form->bind($this->formData);
				$this->data['child']['person_contact'] = CompanyPersonContactsTable::getDataList(array('filter_enabled' => '1', 'filter_company_id'=>$this->data['id']));
			}
		}
		$this->setVar('form', $form);
		//print_r($this->data);die;
	}
	#ACTION FOR EACH ROW
	private function setAction(){
		$action = array();
		$action[]=array(
				'href'	=>	'?action_name='.ADMIN_DEFINED::EDIT_RECORD.'&id=%s',
				'text'	=>	__('action_edit',null,'table_list'),
				'title' =>	__('action_edit',null,'table_list'),
		);
	
		$this->data['action_list']	= $action;
	}
}